package com.cjq.system.controller;

import com.cjq.common.core.utils.poi.ExcelUtil;
import com.cjq.common.core.web.controller.BaseController;
import com.cjq.common.core.web.domain.AjaxResult;
import com.cjq.common.core.web.page.TableDataInfo;
import com.cjq.common.log.annotation.Log;
import com.cjq.common.log.enums.BusinessType;
import com.cjq.common.security.annotation.RequiresPermissions;
import com.cjq.common.security.utils.SecurityUtils;
import com.cjq.system.domain.SysShop;
import com.cjq.system.service.ISysShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 商店信息操作处理
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/shop")
public class SysShopController extends BaseController
{
    @Autowired
    private ISysShopService shopService;

    /**
     * 获取商店列表
     */
    @RequiresPermissions("system:shop:list")
    @GetMapping("/list")
    public TableDataInfo list(SysShop shop)
    {
        startPage();
        List<SysShop> list = shopService.selectShopList(shop);
        return getDataTable(list);
    }

    @Log(title = "商店管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:shop:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysShop shop)
    {
        List<SysShop> list = shopService.selectShopList(shop);
        ExcelUtil<SysShop> util = new ExcelUtil<SysShop>(SysShop.class);
        util.exportExcel(response, list, "商店数据");
    }

    /**
     * 根据商店编号获取详细信息
     */
    @RequiresPermissions("system:shop:query")
    @GetMapping(value = "/{shopId}")
    public AjaxResult getInfo(@PathVariable Long shopId)
    {
        return success(shopService.selectShopById(shopId));
    }

    /**
     * 新增商店
     */
    @RequiresPermissions("system:shop:add")
    @Log(title = "商店管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysShop shop)
    {
        if (!shopService.checkShopNameUnique(shop))
        {
            return error("新增商店'" + shop.getShopName() + "'失败，商店名称已存在");
        }
        else if (!shopService.checkShopCodeUnique(shop))
        {
            return error("新增商店'" + shop.getShopName() + "'失败，商店编码已存在");
        }
        shop.setCreateBy(SecurityUtils.getUsername());
        return toAjax(shopService.insertShop(shop));
    }

    /**
     * 修改商店
     */
    @RequiresPermissions("system:shop:edit")
    @Log(title = "商店管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysShop shop)
    {
        if (!shopService.checkShopNameUnique(shop))
        {
            return error("修改商店'" + shop.getShopName() + "'失败，商店名称已存在");
        }
        else if (!shopService.checkShopCodeUnique(shop))
        {
            return error("修改商店'" + shop.getShopName() + "'失败，商店编码已存在");
        }
        shop.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(shopService.updateShop(shop));
    }

    /**
     * 删除商店
     */
    @RequiresPermissions("system:shop:remove")
    @Log(title = "商店管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{shopIds}")
    public AjaxResult remove(@PathVariable Long[] shopIds)
    {
        return toAjax(shopService.deleteShopByIds(shopIds));
    }

    /**
     * 获取商店选择框列表
     */
    @GetMapping("/optionselect")
    public AjaxResult optionselect()
    {
        List<SysShop> shops = shopService.selectShopAll();
        return success(shops);
    }
}
